package com.user.seat.dao;

import com.user.seat.domain.*;
import com.user.seat.mapper.SeatNumberMapper;
import com.user.seat.mapper.SeatStatusMapper;
import com.user.seat.mapper.SeatUserMapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author jimmy
 * Date  2022/4/21 14:47
 * @version 1.0
 */
@Service
public class UserSeatDao {

    private final SeatUserMapper seatUserMapper;

    public UserSeatDao(SeatUserMapper seatUserMapper){
        this.seatUserMapper = seatUserMapper;
    }

    public List<SeatUser> selectUser(SeatUser seatUser){
        SeatUserExample example = new SeatUserExample();
        SeatUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(seatUser.getUserName());
        return seatUserMapper.selectByExample(example);
    }

    public SeatUser selectUserById(String id){
        return seatUserMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    public int registerUser(SeatUser seatUser){
        seatUser.setCreatedTime(new Date());
        seatUser.setUpdatedTime(new Date());
        seatUser.setNikName(seatUser.getUserName());
        seatUser.setPassword(DigestUtils.md5DigestAsHex(seatUser.getPassword().getBytes()));
        return seatUserMapper.insert(seatUser);
    }

    public int updateNikName(SeatUser seatUser){
        SeatUserExample example = new SeatUserExample();
        SeatUserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(seatUser.getId());
        seatUser.setUpdatedTime(new Date());
        return seatUserMapper.updateByExample(seatUser,example);
    }



    public ResultInfo verifyUser(SeatUser seatUser){
        ResultInfo resultInfo = new ResultInfo();
        boolean result = false;
        String md5Password = "";
        if (seatUser.getPassword()!=null){
            md5Password = DigestUtils.md5DigestAsHex(seatUser.getPassword().getBytes());
        }


        SeatUserExample example = new SeatUserExample();
        SeatUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(seatUser.getUserName());
        List<SeatUser> seatUserList =  seatUserMapper.selectByExample(example);
        if (seatUserList!=null && seatUserList.size()<1){
            //1.校验是否存在

            //2.校验密码
            if (md5Password.equalsIgnoreCase(seatUserList.get(0).getPassword())){
                resultInfo.setCode(200);
                result = true;
            }else {
                resultInfo.setMsg("密码错误");
                resultInfo.setCode(403);
            }

        }else {
            resultInfo.setMsg("用户不存在");
            resultInfo.setCode(403);
        }

        return resultInfo;
    }


}
